package cn.szsys.integration.config;

import cn.szsys.integration.constants.CacheConstant;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author Ryan
 * @since 2025-05-15
 **/
@Configuration
public class CaffeineConfig {
    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();

        // 默认全局配置（适用于所有未单独配置的缓存）
        cacheManager.setCaffeine(Caffeine.newBuilder()
                //初始化容量
                .initialCapacity(100)
                //最大容量
                .maximumSize(500)
                .expireAfterWrite(30, TimeUnit.MINUTES));

        // 为特定缓存名称（如 "accessor"）单独配置
        cacheManager.registerCustomCache(CacheConstant.CACHE_NAME_ACCESSOR,
                Caffeine.newBuilder()
                        .maximumSize(50)
                        .expireAfterAccess(30, TimeUnit.MINUTES)
                        .build());

        return cacheManager;
    }
}
